$(function() {
    if ($('input[name="error"]').val() === "") {
        $("#submit_address").click(function() {

            if (checkValidate('error-msg')) {
                $("#submit_address").attr("disabled", "disabled");

                $.post("webservice/guest/updateRequestAddress.php", {
                    address: $('input[name="address"]').val(),
                    city: $('input[name="city"]').val(),
                    state: $('input[name="state"]').val(),
                    zipcode: $('input[name="zipcode"]').val(),
                    guest_id: $('input[name="guest_id"]').val(),
                    event_id: $('input[name="event_id"]').val()
                }, function(res) {
                    var msgEr = '';
                    var num_milliseconds = 3000;
                    if (res.error == 1) {
                        msgEr = res.msg;
                        $('#error-msg').show().html(msgEr);
                        hideError_2('error-msg', num_milliseconds);
                    } else {
                        alert(res.msg);
                         window.location = domain;
                    }
                    
                    $("#submit_address").removeAttr("disabled");
                }, "json");
            }

        });
    }
});
function checkValidate(id_append) {
   var  numericRegex = /^[0-9-]+$/;
    var check_char = /^[a-zA-Z]+$/;
    // error msg depends on the language
    var errormsg = new Array();
    errormsg[1] = 'The %s field is required.';
    errormsg[2] = 'Please input your %s';
    errormsg[3] = 'The %s field must contain only numbers.';
    errormsg[4] = 'This %s address is invalid.';
    errormsg[5] = '%s is too short. %s2 must be at least %number characters';
    errormsg[6] = 'Confirm password does not match password';
    errormsg[7] = 'The %s field must not exceed %number characters in length.';
    errormsg[8] = 'This %s is invalid.';
    //show msg
    var msgEr = '';
    var valueStr = '';
    var num_milliseconds = 3000;
  
    if ($('input[name="address"]').val() == '' || $('input[name="address"]').val() == 'Address' ) {
        msgEr = errormsg[2].replace('%s', 'street address');
        $('#' + id_append + '').show().html(msgEr);
        hideError(id_append, $('input[name="address"]'), num_milliseconds);
        return false;
    } 
    if ($('input[name="city"]').val() == '' || $('input[name="city"]').val() == 'City') {
        msgEr = errormsg[2].replace('%s', 'city');
        $('#' + id_append + '').show().html(msgEr);
        hideError(id_append, $('input[name="city"]'), num_milliseconds);
        return false;
    } 
    if ($('input[name="state"]').val() == '' || $('input[name="state"]').val() == 'State') {
        msgEr = errormsg[2].replace('%s', 'state');
        $('#' + id_append + '').show().html(msgEr);
        hideError(id_append, $('input[name="state"]'), num_milliseconds);
        return false;
    } 
    if ($('input[name="zipcode"]').val() == '' || $('input[name="zipcode"]').val() == 'Zipcode') {
        msgEr = errormsg[2].replace('%s', 'zipcode');
        $('#' + id_append + '').show().html(msgEr);
        hideError(id_append, $('input[name="zipcode"]'), num_milliseconds);
        return false;
    } else {
        valueStr = $('input[name="zipcode"]').val();
        if (!numericRegex.test(valueStr)) {
            $('#' + id_append + '').show().html("Zipcode can only contain numeric");
            hideError(id_append, $('input[name="zipcode"]'), num_milliseconds);
            return false;
        } 

    }
    return true;
}
function hideError(id_append, item, num_milliseconds) {
    item.css("color", "red");
    setTimeout(function() {
        $('#' + id_append + '').fadeOut('fast', function() {
        });
        $('#' + id_append + '').hide();
    }, num_milliseconds);
}